/**
 * 
 */
package com.zd.ticketing.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zd.common.bean.model.UserInfo;
import com.zd.common.common.constant.CommonConstant;
import com.zd.common.common.utils.EncryptUtil;
import com.zd.common.common.utils.LogUtils;
import com.zd.common.common.utils.StringUtils;
import com.zd.common.common.utils.WeixinApiUtils;
import com.zd.ticketing.bean.vo.ScenicSpotDetailVo;
import com.zd.ticketing.bean.vo.TicketInfoVo;
import com.zd.ticketing.service.IScenicSpotService;
import com.zd.ticketing.service.ITicketInfoService;
import com.zd.ticketing.service.IUserInfoService;

/**
 * 景区控制类
 * 
 * @author lishi
 * @date 2018年11月12日 下午6:53:27
 */
@Controller
@RequestMapping("scenicSpot/ticket")
public class ScenicSpotTicketController {
	
	@Autowired
	private IScenicSpotService scenicSpotService;
	
	@Autowired
	private ITicketInfoService ticketInfoService;
	
	@Autowired
	private IUserInfoService userInfoService;

	/**
	 * 购票首页
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/index")
	public ModelAndView index(String appId, String openId,String nickname ,String img) throws Exception {
		ModelAndView mv = new ModelAndView();
		
		ScenicSpotDetailVo scenicSpotDetailVo = scenicSpotService.getScenicSpotDetail(appId);
		List<TicketInfoVo> ticketInfoList = ticketInfoService.getScenicSpotTicketList(scenicSpotDetailVo.getId());
		UserInfo userInfo = userInfoService.findOpenId(scenicSpotDetailVo.getId(), openId, nickname, img);
		
		mv.addObject("userInfo", userInfo);
		mv.addObject("scenicSpotDetailVo", scenicSpotDetailVo);
		mv.addObject("ticketInfoList", ticketInfoList);
		
		//TODO
		/*
		 * 1、获取景区基本信息
		 * 2、获取景区票相关信息
		 * 3、获取用户相关信息（取票人，常用联系人）
		 */
		mv.setViewName("ticket/index");
		return mv;
	}

	/**
	 * 订单展示页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/orderIndex")
	public ModelAndView orderIndex(String appId, String openId, String touristJson, HttpServletRequest request)
			throws Exception {
		ModelAndView mv = new ModelAndView();
		
		//TODO
		/*
		 * 1、解析json，获取旅客信息，取票人信息，总票价
		 * 2、jssdk签名
		 */
		touristJson = EncryptUtil.decrypt(touristJson, CommonConstant.AES_KEY);
		LogUtils.debug(touristJson);
		Map<String, Object> resultMap = userInfoService.analysisTouristJson(touristJson ,true);
		userInfoService.savePayOpenId(openId, Integer.valueOf(resultMap.get("userId").toString()));
		resultMap.put("appid", appId);
		resultMap.put("openid", openId);
		resultMap.put("touristJson", touristJson);
		
		String url = request.getRequestURL().toString();
		String queryStr = request.getQueryString();
		if (!StringUtils.isBank(queryStr)) {
			url += "?" + queryStr;
		}
		
		JSONObject signatureObj = WeixinApiUtils.jssdk_signture(url, resultMap.get("payAccountId").toString());
		
		
		mv.addObject("signature", signatureObj);
		mv.addObject("resultMap", resultMap);
		
		LogUtils.info(signatureObj);
		LogUtils.info(resultMap);
		mv.setViewName("ticket/orderIndex");
		return mv;
	}
}
